<?php

namespace zhtfrontend\modules\cardmanage\controllers;

use common\components\helpers\Fun;
use common\models\datas\CardAttr;
use common\models\datas\CardReSend;
use common\models\datas\Cards;
use common\models\datas\CardsAttr;
use common\models\datas\CardsBatch;
use common\models\datas\CardSend;
use common\models\datas\CardsIssuerinfor;
use common\models\datas\CardsResend;
use common\models\datas\CardsSend;
use common\models\datas\IsuserInfo;
use yii\helpers\ArrayHelper;
use zhtfrontend\modules\cardmanage\components\rest\ActiveController;
use yii\web\Response;

class CardController extends ActiveController
{
    public $modelClass = 'common\models\datas\CardsSend';

    public $serializer = [
        'class' => 'zhtfrontend\modules\cardmanage\components\rest\Serializer',
        'collectionEnvelope' => 'items',
    ];

    public function behaviors()
    {
        $behaviors = parent::behaviors();
        $behaviors['contentNegotiator']['formats']['text/html'] = Response::FORMAT_JSON;
        return $behaviors;
    }
    public function actions()
    {
        $actions = parent::actions();
        unset($actions['index'], $actions['create'], $actions['update'], $actions['delete'], $actions['view']);
        return $actions;
    }
    //获取卡批次
    public function actionBatchlist(){
        $data = \Yii::$app->request->get();
        $user_id= ArrayHelper::keyExists('user_id', $data) ? $data['user_id'] : '';
        $level= ArrayHelper::keyExists('level', $data) ? $data['level'] : '';
        $site=ArrayHelper::keyExists('site', $data) ? $data['site'] : 2;
        if($site=='qdlcaks'){
            $site=2;
        }
        if ($level==0){
            $batch_list=CardsBatch::find()->where(['applicant_id'=>$user_id,'status'=>1,'is_delete'=>0,'site_id'=>$site])->all();
            $card_batch_list=[];
            foreach ($batch_list as $key=>$val){
                $cardno_begin_id=Cards::find()->where(['id'=>$val->cardno_begin])->one();
                $cardno_end_id=Cards::find()->where(['id'=>$val->cardno_end])->one();
                $card_batch_list[$key]=[
                    'list_id'=>$val->id,
                    'title'=>$val->title,
                    'approver_number'=>(int)$val->approver_number,
                    'grant_time'=>date('Y-m-d', $val->grant_time),
                    'cardno_begin'=>$cardno_begin_id->card_no,
                    'cardno_end'=>$cardno_end_id->card_no,
                    'card_send'=>(int)$val->card_send,
                    'card_remaining'=>(int)$val->approver_number-(int)$val->card_send,
                ];
            }
            return ['items'=>$card_batch_list];
        }else{
            $send_list=CardsSend::find()->where(['reissuser_id'=>$user_id,'is_delete'=>0])->all();
            $card_send_list=[];
            foreach ($send_list as $k=>$v){
                $cardno_begin_id=Cards::find()->where(['id'=>$v->cardno_begin])->one();
                $cardno_end_id=Cards::find()->where(['id'=>$v->cardno_end])->one();
                $card_send_list[$k]=[
                    'list_id'=>$v->id,
                    'title'=>isset($v->cardsBatch->title)?$v->cardsBatch->title:'',
                    'approver_number'=>(int)$v->number,
                    'grant_time'=>date('Y-m-d', $v->created_at),
                    'cardno_begin'=>$cardno_begin_id->card_no,
                    'cardno_end'=>$cardno_end_id->card_no,
                    'card_send'=>(int)$v->card_send,
                    'card_remaining'=>(int)$v->number-(int)$v->card_send,

                ];
            }
            return ['items'=>$card_send_list];
        }
    }
    //下面是一级发卡人发卡的信息
    public function actionSendinfor(){
        $data = \Yii::$app->request->get();
        $batch_id=ArrayHelper::keyExists('batch_id', $data) ? $data['batch_id'] : '';
        $site=ArrayHelper::keyExists('site', $data) ? $data['site'] : 2;
        if($site=='qdlcaks'){
            $site=2;
        }
        if($batch_id!==''){
            $card_issuerinfor=CardsIssuerinfor::find()->where(['level'=>1,'is_delete'=>0])->asArray()->all();
            $issuerinfor=ArrayHelper::map($card_issuerinfor, 'id', 'name');
            $bacth_infor=CardsBatch::find()->where(['id'=>$batch_id])->one();
            //开始编号需要加上已经发卡了的编号
            $remaining_cards_begin=CardsAttr::find()->where(['batch_id'=>$batch_id,'reissuser_id'=>0])->orderBy('id ASC')->one();
            $remaining_cards=CardsAttr::find()->where(['batch_id'=>$batch_id,'reissuser_id'=>0])->orderBy('id ASC')->all();
            $remaining_cards_count=count($remaining_cards);
            $send_infor=[
                'issuerinfor'=>$issuerinfor,
                'cardno_begin'=>$remaining_cards_begin->card_no,
                'remaining_cards_count'=>$remaining_cards_count,
            ];
            return ['items'=>$send_infor];

        }else{
            return ['statusCode' =>0, 'message' => 'error', 'result' =>'批次信息不存在'];
        }
    }
    //下面是一级发卡人的发卡操作
    public function actionSend(){
        $modelClass = $this->modelClass;
        $data = \Yii::$app->request->get();
        $batch_id=ArrayHelper::keyExists('batch_id', $data) ? $data['batch_id'] : '';
        $cards_issuerinfor=ArrayHelper::keyExists('cardIssuerinfor', $data) ? $data['cardIssuerinfor'] : '';
        $cardno_begin=ArrayHelper::keyExists('cardno_begin', $data) ? $data['cardno_begin'] : '';
        $user_id=ArrayHelper::keyExists('user_id', $data) ? $data['user_id'] : '';
        $cardCount=ArrayHelper::keyExists('cardCount', $data) ? $data['cardCount'] : '';
        $cardDivided=ArrayHelper::keyExists('cardDivided', $data) ? $data['cardDivided'] : '';
        if ($batch_id!==''){
            //首先往card_attr表里面修改存数据
            $card_batch=CardsBatch::find()->where(['id'=>$batch_id])->one();
            $all_cards=CardsAttr::find()->where(['batch_id'=>$batch_id,'reissuser_id'=>0])->all();
            $send_count=(int)$cardCount;
            if ($send_count>count($all_cards)){
                return ['statusCode' =>201, 'message' => 'error', 'result' =>'此批次卡数不够'];
            }else{
                $send_card_attr=CardsAttr::find()->where(['batch_id'=>$batch_id,'reissuser_id'=>0])->orderBy('id ASC')->limit($cardCount)->all();
                $cardno_begin_id=CardsAttr::find()->where(['batch_id'=>$batch_id,'card_no'=>$cardno_begin])->one();
                $card_batch->card_send = (int)$card_batch->card_send+(int)$cardCount;
                if ($card_batch->save(false)) {
                    //下面就是如果上面card_attr表和card_batch表保存成功的话，需要往card_send表里面插入一条数据
                    $model = new $modelClass;
                    $model->batch_id = (int)$card_batch->id;
                    $model->issuser_id=(int)$card_batch->applicant_id;
                    $model->reissuser_id=(int)$cards_issuerinfor;
                    $model->number=(int)$cardCount;
                    $model->card_send=0;
                    $model->cardno_begin=isset($cardno_begin_id)?$cardno_begin_id->card_id:0;
                    $model->cardno_end=isset($send_card_attr[$cardCount-1])?$send_card_attr[$cardCount-1]->card_id:0;
                    $model->commission_rate=0;
                    $model->proportion=$cardDivided;
                    $model->is_withdraw=0;
                    $model->created_at=strtotime(date('Y-m-d'));
                    if ($model->save(false)){
                        foreach ($send_card_attr as $key=>$val) {
                            $val->reissuser_id = $cards_issuerinfor;
                            $val->send_id=$model->id;
                            $val->save(false);
                        }
                        return ['items'=>$model];
                    }
                }else{
                    return ['statusCode' =>203, 'message' => 'error', 'result' =>'批次发卡数量保存失败'];
                }
                return ['items'=>$card_batch];
            }
        }else{
            return ['statusCode' =>0, 'message' => 'error', 'result' =>'批次信息不存在'];
        }
    }
    //下面是一级发卡人下面的批次之后的发卡列表
    public function actionSendlist(){
        $modelClass = $this->modelClass;
        $data = \Yii::$app->request->get();
        $batch_id=ArrayHelper::keyExists('id', $data) ? $data['id'] : '';
        $user_id=ArrayHelper::keyExists('user_id', $data) ? $data['user_id'] : '';
        $level=ArrayHelper::keyExists('level', $data) ? $data['level'] : '';
        if ($batch_id!==''){
            $batch_send=$modelClass::find()->where(['batch_id'=>$batch_id,'is_delete'=>0])->orderBy('id ASC')->all();
            $batch_send_data=[];
            foreach ($batch_send as $key=>$val){
                $reissuer=CardsIssuerinfor::find()->where(['id'=>$val->reissuser_id])->one();
                $cardno_begin=Cards::findOne($val->cardno_begin);
                $cardno_end=Cards::findOne($val->cardno_end);
                $batch_send_data[$key]=[
                    'id'=>$val->id,
                    'batch_id'=>$val->batch_id,
                    'reissuer'=>$reissuer->name,
                    'number'=>$val->number,
                    'cardno_begin'=>$cardno_begin->card_no,
                    'cardno_end'=>$cardno_end->card_no,
                    'created_at'=>date('Y-m-d',$val->created_at),
                ];
            }
            return ['items'=>$batch_send_data];
        }else{
            return ['statusCode' =>0, 'message' => 'error', 'result' =>'批次信息不存在'];
        }
    }
    //下面这个发卡得详情页面,主要是看发给这个人之后，已经发卡了多少张，然后激活率啥的
    public function actionListdetail(){
        $data = \Yii::$app->request->get();
        $send_id=ArrayHelper::keyExists('id', $data) ? $data['id'] : '';
        $user_id=ArrayHelper::keyExists('user_id', $data) ? $data['user_id'] : '';
        $level=ArrayHelper::keyExists('level', $data) ? $data['level'] : '';
        if ($send_id!==''){
            if($level==0){//如果是一级发卡人
                $card_send=CardsSend::find()->where(['id'=>$send_id])->all();
                $card_resend=CardsResend::find()->where(['card_send_id'=>$send_id,'is_delete'=>0])->all();
                $list_detail_data=[];
                foreach ($card_send as $key=>$val){
                    $reissuer=CardsIssuerinfor::find()->where(['id'=>$val->reissuser_id])->one();
                    $card_send_no=CardsAttr::find()->where(['between', 'card_id', $val->cardno_begin, $val->cardno_end])->andWhere(['and', 'customer_id!=0', 'active_time!=0'])->all();
                    $active_rate=(int)count($card_send_no)/(int)$val->number*100;
                    $list_detail_data[$key]=[
                        'id'=>$send_id,
                        'reissuer'=>isset($reissuer)?$reissuer->name:'',
                        'send_number'=>$val->card_send,
                        'active_rate'=>$active_rate,
                    ];
                }
                return ['items'=>$list_detail_data];
            }else{
                $relist_detail_data=[];
                $card_resend=CardsResend::find()->where(['card_send_id'=>$send_id,'is_delete'=>0])->all();
                foreach ($card_resend as $key=>$val){
                    $card_send_no=CardsAttr::find()->where(['between', 'card_id', $val->cardno_begin, $val->cardno_end])->andWhere(['and', 'customer_id!=0', 'active_time!=0'])->all();
                    $active_rate=(int)count($card_send_no)/(int)$val->number*100;
                    $relist_detail_data[$key]=[
                        'id'=>$val->id,
                        'reissuer'=>$val->salesman,
                        'send_number'=>$val->number,
                        'active_rate'=>$active_rate,
                    ];
                }
                return ['items'=>$relist_detail_data];
            }
        }else{
            return ['statusCode' =>0, 'message' => 'error', 'result' =>'发卡信息不存在'];
        }
    }
    //下面是二级发卡人的发卡
    public function actionResendinfor(){
        $data = \Yii::$app->request->get();
        $send_id=ArrayHelper::keyExists('send_id', $data) ? $data['send_id'] : '';
        if ($send_id!==''){
            $card_send=CardsSend::find()->where(['id'=>$send_id])->one();
            //开始编号需要加上已经发卡了的编号
            $remaining_cards_begin=CardsAttr::find()->where(['send_id'=>$send_id,'salesman'=>null,'is_delete'=>0])->orderBy('id ASC')->one();
            $remaining_cards=CardsAttr::find()->where(['send_id'=>$send_id,'salesman'=>null])->orderBy('id ASC')->all();
            $remaining_cards_count=count($remaining_cards);
            $send_infor=[
                'cardno_begin'=>$remaining_cards_begin->card_no,
                'remaining_cards_count'=>$remaining_cards_count,
            ];
            return ['items'=>$send_infor];
        }else{
            return ['statusCode' =>0, 'message' => 'error', 'result' =>'发卡信息不存在'];
        }
    }
    public function actionResend(){
        $modelClass = $this->modelClass;
        $data = \Yii::$app->request->get();
        $send_id=ArrayHelper::keyExists('send_id', $data) ? $data['send_id'] : '';
        $recard_issuerinfor=ArrayHelper::keyExists('recard_issuerinfor', $data) ? $data['recard_issuerinfor'] : '';
        $recard_telephone=ArrayHelper::keyExists('recard_telephone', $data) ? $data['recard_telephone'] : '';
        $cardno_begin=ArrayHelper::keyExists('cardno_begin', $data) ? $data['cardno_begin'] : '';
        $user_id=ArrayHelper::keyExists('user_id', $data) ? $data['user_id'] : '';
        $recard_number=ArrayHelper::keyExists('recard_number', $data) ? $data['recard_number'] : '';
        $recard_divided=ArrayHelper::keyExists('recard_divided', $data) ? $data['recard_divided'] : '';
        if ($send_id!==''){
            //首先往card_attr表里面修改存数据
            $card_send=CardsSend::findOne($send_id);
            $all_cards=CardsAttr::find()->where(['send_id'=>$send_id,'salesman'=>null,'is_delete'=>0])->all();
            $resend_count=(int)$recard_number;
            if ($resend_count>count($all_cards)){
                return ['statusCode' =>201, 'message' => 'error', 'result' =>'此次卡数不够'];
            }else{
                $resend_card_attr=CardsAttr::find()->where(['send_id'=>$send_id,'salesman'=>null])->orderBy('id ASC')->limit($recard_number)->all();
                $cardno_begin_id=CardsAttr::find()->where(['send_id'=>$send_id,'card_no'=>$cardno_begin])->one();
                $card_send->card_send = (int)$card_send->card_send+(int)$recard_number;
                if ($card_send->save(false)) {
                    //如果是上面的card_send表保存成功的话，需要往card_resend表里面插入一条疾苦
                    $model = new CardsResend();
                    $model->batch_id = (int)$card_send->batch_id;
                    $model->card_send_id=(int)$card_send->id;
                    $model->issuser_id=(int)$card_send->reissuser_id;
                    $model->salesman=$recard_issuerinfor;
                    $model->telephone=$recard_telephone;
                    $model->number=(int)$recard_number;
                    $model->cardno_begin=isset($cardno_begin_id)?$cardno_begin_id->card_id:0;
                    $model->cardno_end=isset($resend_card_attr[$recard_number-1])?$resend_card_attr[$recard_number-1]->card_id:0;
                    $model->proportion=$recard_divided;
                    $model->created_at=strtotime(date('Y-m-d'));
                    if ($model->save(false)){
                        foreach ($resend_card_attr as $key=>$val) {
                            $val->salesman = $recard_issuerinfor;
                            $val->save(false);
                        }
                        return ['items'=>$model];
                    }
                }else{
                    return ['statusCode' =>203, 'message' => 'error', 'result' =>'发卡数量保存失败'];
                }
                return ['items'=>$resend_card_attr];
            }
        }else{
            return ['statusCode' =>0, 'message' => 'error', 'result' =>'批次信息不存在'];
        }
    }
    public function actionActivedetail(){
        $data = \Yii::$app->request->get();
        $user_id=ArrayHelper::keyExists('user_id', $data) ? $data['user_id'] : '';
        $level=ArrayHelper::keyExists('level', $data) ? $data['level'] : '';
        $id=ArrayHelper::keyExists('id', $data) ? $data['id'] : '';
        if($id!==''){
            if($level==0){
                $send=CardsSend::findOne($id);
                $cardno_begin=(int)$send->cardno_begin;
                $cardno_end=(int)$send->cardno_end;
                $card_attr=CardsAttr::find()->where(['between', 'card_id', $cardno_begin, $cardno_end])->all();
                $card_attr_active=CardsAttr::find()->where(['between', 'card_id', $cardno_begin, $cardno_end])->andWhere(['status'=>1])->all();
                $cardno_begin_no=CardsAttr::find()->where(['card_id'=>$cardno_begin])->one();
                $cardno_end_no=CardsAttr::find()->where(['card_id'=>$cardno_end])->one();
                $card_no=[
                    'cardno_begin_no'=>$cardno_begin_no->card_no,
                    'cardno_end_no'=>$cardno_end_no->card_no,
                    'card_attr_active'=>count($card_attr_active),
                ];
                $card_attr_data=[];
                foreach ($card_attr as $key=>$val){
                    switch ($val->status){
                        case 0:
                            $val->status='未激活';break;
                        case 1:
                            $val->status='已激活';break;
                    }
                    $card_attr_data[$key]=[
                        'card_id'=>$val->card_id,
                        'card_no'=>$val->card_no,
                        'v_code'=>$val->v_code,
                        'status'=>$val->status,
                    ];
                }
                $card_array_data=[
                    'card_no'=>$card_no,
                    'card_attr_data'=>$card_attr_data,
                ];
                return ['items'=>$card_array_data];

            }else{
                $resend=CardsResend::findOne($id);
                $cardno_begin=(int)$resend->cardno_begin;
                $cardno_end=(int)$resend->cardno_end;
                $card_attr=CardsAttr::find()->where(['between', 'card_id', $cardno_begin, $cardno_end])->all();
                $card_attr_active=CardsAttr::find()->where(['between', 'card_id', $cardno_begin, $cardno_end])->andWhere(['status'=>1])->all();
                $cardno_begin_no=CardsAttr::find()->where(['card_id'=>$cardno_begin])->one();
                $cardno_end_no=CardsAttr::find()->where(['card_id'=>$cardno_end])->one();
                $card_no=[
                    'cardno_begin_no'=>$cardno_begin_no->card_no,
                    'cardno_end_no'=>$cardno_end_no->card_no,
                    'card_attr_active'=>count($card_attr_active),
                ];
                $card_attr_data=[];
                foreach ($card_attr as $key=>$val){
                    switch ($val->status){
                        case 0:
                            $val->status='未激活';break;
                        case 1:
                            $val->status='已激活';break;
                    }
                    $card_attr_data[$key]=[
                        'card_id'=>$val->card_id,
                        'card_no'=>$val->card_no,
                        'v_code'=>$val->v_code,
                        'status'=>$val->status,
                    ];
                }
                $card_array_data=[
                    'card_no'=>$card_no,
                    'card_attr_data'=>$card_attr_data,
                ];
                return ['items'=>$card_array_data];
            }
        }else{
            return ['statusCode' =>0, 'message' => 'error', 'result' =>'信息不存在'];
        }
    }


    //下面的代码是之前的代码了，没啥用处
    public function actionBatchlistsecond()
    {
        $modelClass = $this->modelClass;
        $data = \Yii::$app->request->get();
        $user_id = ArrayHelper::keyExists('user_id', $data) ? $data['user_id'] : '';
        $user_parent = ArrayHelper::keyExists('user_parent', $data) ? $data['user_parent'] : '';
        $batch_id = ArrayHelper::keyExists('batch_id', $data) ? $data['batch_id'] : '';
        $site=ArrayHelper::keyExists('site', $data) ? $data['site'] : 2;
        if($site=='qdlcaks'){
            $site=2;
        }
        $card_send = $modelClass::find()->where(['salesman_id' => $user_id, 'site_id' => $site, 'batch_id' => $batch_id])->all();
        $card_send_arr = [];
        if ($user_parent == 0) {
            foreach ($card_send as $k => $v) {
                if ($v->parent_id == 0 || $v->is_withdraw != 0) {
                    $card_re_send = $modelClass::find()->where(['batch_id' => $v->batch_id, 'parent_id' => $v->id, 'site_id' => $site])->all();
                    $surplus_count = 0;
                    $active_re_count = 0;
                    foreach ($card_re_send as $rk => $rv) {
                        $surplus_count += $rv->number;
                        $active_count = CardAttr::find()->where(['site_id' => $site, 'status' => 1])->andWhere(['between', 'card_id', $rv->card_start, $rv->card_end])->count(1);
                        $active_re_count += $active_count;
                    }
                    $surplus = $v->number - $surplus_count;

                    $card_send_arr[$k] = [
                        'card_send_count' => $surplus_count,
                        'surplus' => $surplus,
                        'batch' => $v->cardBatch->title,
                        'created_at' => date('Y-m-d', $v->created_at),
                        'active' => $active_re_count == 0 ? 0 : round($active_re_count / $surplus_count, 2) * 100,
                        'id' => $v->id,
                        'is_withdraw' => $v->is_withdraw,
                        'card_start' => $v->card_start,
                        'card_end' => $v->card_end,
                        'reisuser' => $user_parent,
                    ];
                }
            }
        } else {
            //二次发卡人
            foreach ($card_send as $k => $v) {
                $card_re_send = CardReSend::find()->where(['batch_id' => $v->batch_id, 'issuser_id' => $v->salesman_id, 'card_send_id' => $v->id])->all();
                $surplus_count = 0;
                $active_re_count = 0;
                foreach ($card_re_send as $rk => $rv) {
                    $surplus_count += $rv->number;
                    $active_count = CardAttr::find()->where(['site_id' => $this->site_id, 'status' => 1])->andWhere(['between', 'card_id', $rv->card_start, $rv->card_end])->count(1);
                    $active_re_count += $active_count;
                }
                $surplus = $v->number - $surplus_count;
                if ($v->parent_id == 0 && $v->is_withdraw == 0) {
                    $is_withdraw = 0;
                } else {
                    $is_withdraw = 1;
                }

                $card_send_arr[$k] = [
                    'card_send_count' => $surplus_count,//已发卡
                    'surplus' => $surplus,//剩余
                    'batch' => $v->cardBatch->title,
                    'created_at' => date('Y-m-d', $v->created_at),
                    'active' => $active_re_count == 0 ? 0 : round($active_re_count / $surplus_count, 2) * 100,
                    'id' => $v->id,
                    'card_start' => $v->card_start,
                    'card_end' => $v->card_end,
                    'is_withdraw' => $is_withdraw,
                ];
            }
        }
        return ['items'=>$card_send_arr];
    }
    //下面是二级发卡页面下面的查看详情
    public function actionCarddetail()
    {
        $modelClass = $this->modelClass;
        $data = \Yii::$app->request->get();
        $user_id = ArrayHelper::keyExists('user_id', $data) ? $data['user_id'] : '';//登录用户的id
        $user_parent = ArrayHelper::keyExists('user_parent', $data) ? $data['user_parent'] : '';//登录的用户的parent_id
        $card_id= ArrayHelper::keyExists('card_id', $data) ? $data['card_id'] : '';//当前批次的id
        $site=ArrayHelper::keyExists('site', $data) ? $data['site'] : 2;
        if($site=='qdlcaks'){
            $site=2;
        }
        $card_send_arr = [];
        if ($user_parent != 0) {
            $card_re_send = CardReSend::find()->where(['card_send_id' => $card_id])->all();
            if (empty($card_re_send)) {
                return ['statusCode' => 0, 'message' => 'error', 'result' => '未发卡'];
            }
            foreach ($card_re_send as $k => $v) {
                $active = CardAttr::find()->where(['site_id' => $site, 'status' => 1])->andWhere(['between', 'card_id', $v->card_start, $v->card_end])->count(1);
                $card_send_arr[$k] = [
                    'number' => $v->number,
                    'salesman' => $v->salesman,
                    'active' => $active == 0 ? 0 : round($active / $v->number, 2) * 100,
                    're_id' => $v->id,
                    'reisuser' => $user_parent,
                ];
            }
        } else {
            $card_re_send = $modelClass::find()->where(['parent_id' => $card_id])->andWhere(['!=', 'is_withdraw', 1])->all();
            if (empty($card_re_send)) {
                return ['statusCode' => 0, 'message' => 'error', 'result' => '未发卡'];
            }
            foreach ($card_re_send as $k => $v) {
                $active = CardAttr::find()->where(['site_id' => $site, 'status' => 1])->andWhere(['between', 'card_id', $v->card_start, $v->card_end])->count(1);
                $card_send_arr[$k] = [
                    'number' => $v->number,
                    'salesman' => $v->isuserinfo->contact,
                    'active' => $active == 0 ? 0 : round($active / $v->number, 2) * 100,
                    're_id' => $v->id,
                    'reisuser' => $user_parent,
                ];
            }
        }
        return ['items'=>$card_send_arr];
    }
    //下面是填写发卡信息(这个方法是一级发卡人的信息)
       //首先是获取发卡人的信息
    public function actionCardreisuser(){
        $data = \Yii::$app->request->get();
        $user_id = ArrayHelper::keyExists('user_id', $data) ? $data['user_id'] : '';//登录用户的id
        $site=ArrayHelper::keyExists('site', $data) ? $data['site'] : 2;
        if($site=='qdlcaks'){
            $site=2;
        }
        $reisuser_data = [];
        $reisuser_data[0] = '请选择发卡人';
        $isuser = IsuserInfo::findOne([$user_id]);
        $reisuser_data[$isuser->id] = $isuser->contact;
        $reisuser = IsuserInfo::find()->where(['site_id' => $site, 'parent_id' => $user_id])->all();
        foreach ($reisuser as $k => $v) {
            $reisuser_data[$v->id] = $v->contact;
        }
        return ['items'=>$reisuser_data];
    }
    //下面是发卡提交的信息
    public function actionCardsend(){
        $modelClass = $this->modelClass;
        $data = \Yii::$app->request->get();
        $user_id = ArrayHelper::keyExists('user_id', $data) ? $data['user_id'] : '';//登录用户的id
        $site=ArrayHelper::keyExists('site', $data) ? $data['site'] : 2;
        if($site=='qdlcaks'){
            $site=2;
        }
        $card_id=ArrayHelper::keyExists('card_id', $data) ? $data['card_id'] : '';//登录用户的id
        //$card_send_name=ArrayHelper::keyExists('cardSendName', $data) ? $data['cardSendName'] : '';//发卡人姓名
        $card_send_number=ArrayHelper::keyExists('cardNumber', $data) ? $data['cardNumber'] : '';//发卡编号
        $card_send_count=ArrayHelper::keyExists('cardCount', $data) ? $data['cardCount'] : '';//发卡数量
        $card_send_devided=ArrayHelper::keyExists('cardDivided', $data) ? $data['cardDivided'] : '';//发卡分成比例
        $card_send_id=ArrayHelper::keyExists('cardSendId', $data) ? $data['cardSendId'] : '';//发卡分成比例
        $model = new $modelClass;
        $card_send = $modelClass::findOne(['id' => $card_id]);
        $model->number=$card_send_number;
        $model->user_id = $card_send->salesman_id;
        $model->salesman_id=$card_send_id;
        $model->batch_id = $card_send->batch_id;
        $model->card_start = $card_send->card_start + $card_send_count;
        $model->card_end = $model->card_start + $model->number - 1;
        $model->site_id = $site;
        $model->issuser_id = $card_send->issuser_id;
        $model->parent_id = $card_id;
        $model->proportion =$card_send_devided;
        if ($model->save(false)){
            return ['items'=>$model];
        }else{
            return ['statusCode' => 0, 'message' => 'error', 'result' => '发卡失败'];
        }
    }
    //下面是查看分成页面的代码
    public function actionProportion(){

    }
    //下面是查看详情页面的代码
    public function actionActivedetails(){
        $modelClass = $this->modelClass;
        $data = \Yii::$app->request->get();
        $user_id = ArrayHelper::keyExists('user_id', $data) ? $data['user_id'] : '';//登录用户的id
        $site=ArrayHelper::keyExists('site', $data) ? $data['site'] : 2;
        if($site=='qdlcaks'){
            $site=2;
        }
        $user_parent = ArrayHelper::keyExists('user_parent', $data) ? $data['user_parent'] : '';//登录的用户的parent_id
        $re_id=ArrayHelper::keyExists('re_id', $data) ? $data['re_id'] : '';//其实这个地方就是card_id
        if ($user_parent == 0) {
            $card_re_send = $modelClass::findOne($re_id);
            $active_count = CardAttr::find()->where(['between', 'card_id', $card_re_send->card_start, $card_re_send->card_end])->andWhere(['site_id' => $site, 'status' => 1])->count(1);
            $data = [
                'card_start' => $card_re_send->card_start,
                'card_end' => $card_re_send->card_end,
                'active_count' => $active_count,
            ];
            $card_attr = CardAttr::find()->where(['between', 'card_id', $card_re_send->card_start, $card_re_send->card_end])->andWhere(['site_id' =>$site])->all();
            $data_arr = [];
            foreach ($card_attr as $k => $v) {
                $data_arr[$k] = [
                    'card_id' => $v->card_id,
                    'status' => $v->status == 0 ? '未激活' : '已激活',
                    'v_code' => $v->v_code
                ];
            }
        } else {
            $card_re_send = CardReSend::findOne($re_id);
            $active_count = CardAttr::find()->where(['between', 'card_id', $card_re_send->card_start, $card_re_send->card_end])->andWhere(['site_id' => $site, 'status' => 1])->count(1);
            $data = [
                'card_start' => $card_re_send->card_start,
                'card_end' => $card_re_send->card_end,
                'active_count' => $active_count,
            ];
            $card_attr = CardAttr::find()->where(['between', 'card_id', $card_re_send->card_start, $card_re_send->card_end])->andWhere(['site_id' => $site])->all();
            $data_arr = [];
            foreach ($card_attr as $k => $v) {
                $data_arr[$k] = [
                    'card_id' => $v->card_id,
                    'status' => $v->status == 0 ? '未激活' : '已激活',
                    'v_code' => $v->v_code
                ];
            }
        }
        $datas=[
            'data'=>$data,
            'data_arr'=>$data_arr,
        ];
        return ['items'=>$datas];
    }
}